﻿//hash改变后触发
function changePage(){
    var actionName = window.location.hash.replace('#', '');
    actionName = actionName.split('&');
    if(actionName[0] == 'getPackagesList'){
        $('#wst-page1').addClass('wst-page1');
        $('#wst-page2').show();
        $('#wst-footer').hide();
        getPackagesList();
    }else if(actionName[0] == 'brands'){
        $('#wst-page1').addClass('wst-page1');
        $('#wst-page2').show();
        $('#wst-footer').hide();
        getBrands();
    }else{
        $('#wst-page1').show();
        $('#wst-page2').addClass('wst-page2');
        setTimeout(function(){
            $('body').removeClass('ajaxpage-active');
        }, 100);
        setTimeout(function(){
          $('#wst-page1').removeClass('wst-page1');
            $('#wst-footer').show();
            $('#wst-page2').css('display','none');
        }, 500);
    }
}

//优惠套餐页
function getPackagesList(){
    $('#wst-default-loading').modal();
    var goodsId = Number( $('#goodsId').val() );
    var shopId = Number( $('#shopId').val() );
    $.post(WST.U('WebApp/Goods/getPackagesList'), {goodsId: goodsId, shopId: shopId}, function(data){
      var json = WST.toJson(data);
      var template = Handlebars.compile( $('#goodsPakages').text() );
      Handlebars.registerHelper("compare",function(v1,v2,options){//注册一个比较大小的Helper,判断v1是否大于v2
          if(v1 == v2){
              return options.fn(this);
          }else{
              return options.inverse(this);
          }
      });
      var pakages = {'packageList': json, 'defaultImg': WST.DEFAULT_IMG, 'rooturl': WST.ROOT};
      var html = template(pakages);
      $('#wst-page2').html(html);
      $('#wst-default-loading').modal('close');
      $('body').addClass('ajaxpage-active');
        setTimeout(function(){ 
          $('#wst-page1').hide();
          $('#wst-page2').removeClass('wst-page2');
          $(document).scrollTop(0);
        }, 300);
      json = template = pakages = html = null;
        setTimeout(function(){ 
          echo.init();//图片懒加载
        }, 300);
    });
}

//跳转到商品评价页
function viewAppraises(goodsId){
    location.href = WST.U("WebApp/Goods/toGoodsAppraise", "goodsId="+goodsId);
}

//检查商品库存
function checkStock(obj, id, goodsId){
    $('#goodsAttrId').val(id);
    $(obj).addClass('active').siblings().removeClass('active');
    $.post(WST.U('WebApp/Goods/getPriceAttrInfo'), {goodsId:goodsId, id:id}, function(data){
        var json = WST.toJson(data);
        $('#goodsPrice').html('￥'+json.attrPrice);
        if(json.attrStock>0){
          $('#goodsStock').html('有货').css('color','black');
          $('#buynow').addClass('active').removeAttr('disabled');
          $('#add-to-cart').addClass('active').removeAttr('disabled');
        }else{
          $('#goodsStock').html('无货').css('color','red');
          $('#buynow').removeClass('active').attr('disabled', 'disabled');
          $('#add-to-cart').removeClass('active').attr('disabled', 'disabled');
        }
    });
}

//选择商品规格
function selectGoodsAttr(obj, id, goodsId, packageId, diffPrice){
    $(obj).addClass('active').siblings().removeClass('active');
    var defaultGoodsId = Number( $('#goodsId').val() );//当前商品ID
    $.post(WST.U('WebApp/Goods/getPriceAttrInfo'), {goodsId:goodsId, id:id}, function(data){
        var json = WST.toJson(data);
        var $container = $('#package-'+packageId);
        var packagePrice = 0;
        if( defaultGoodsId == goodsId ){
          $container.find('input.goodsInfo').each(function(){
            if( $(this).attr('goodsId') != defaultGoodsId ){
              packagePrice += Number( $(this).attr('goodsPrice') );
            }else{
              //更新当前商品的价格、价格属性
              $(this).attr('goodsPrice', json.attrPrice);
              $(this).attr('goodsAttrId', id);
            }
          });
          $('#packagePrice-'+packageId).html( (packagePrice + Number( json.attrPrice )).toFixed(2) );

          $('#goods-'+packageId+'-'+goodsId).html(json.attrPrice);
        }else{
          var goodsAttrPrice = json.attrPrice - diffPrice;
          $container.find('input.goodsInfo').each(function(){
            if( $(this).attr('goodsId') != defaultGoodsId ){
              packagePrice += goodsAttrPrice;

              //更新当前商品的价格
              $(this).attr('goodsPrice', goodsAttrPrice);
              $(this).attr('goodsAttrId', id);
            }else{
              packagePrice += Number( $(this).attr('goodsPrice') );
            }
          });
          $('#packagePrice-'+packageId).html( packagePrice.toFixed(2) );

          $('#goods-'+packageId+'-'+goodsId).html( goodsAttrPrice.toFixed(2) );
        }
    });
}

//加入商品到购物车
function addGoodsPackageToCart(packageId){
    var $container = $('#package-'+packageId);
    var goodsInfo = [];
    $container.find('input.goodsInfo').each(function(){
      var goodsId = Number( $(this).val() ); 
      var goodsAttrId = Number( $(this).attr('goodsAttrId') ); 
      goodsInfo.push( goodsId + '_' + goodsAttrId + '_1' );
    });

    var param = {};
    param.packageId = packageId;
    param.goodsAttrIds = goodsInfo.join("@");
    param.rnd = Math.random();

    $.post(WST.U('WebApp/Cart/addGoodsPackageToCart'), param, function(data){
        var json = WST.toJson(data);
        if(json.status == 1){
            location.href = WST.U('WebApp/Cart/index');
        }else{
            wstMsg(json.msg);
        }
    });
}

$(document).ready(function(){
    initFooter('home');
});
